<?php

require_once 'lib.php';
require_once 'model/Event.php';
require_once 'lib/ScheduleHandler.php';

// If the key is send via POST, use POST instead of GET.
if(isset($_POST['k'])){
    $_GET = $_POST;
}

$course = $_GET['c'];
$year = $_GET['y'];
$key = $_GET['k'];

// Create the handler, retrieve and parse data.
$h = new ScheduleHandler($course, $year, $key);
$h->retrieve();
$h->parse();

// Get the results
$results = $h->getResults();

// Prepare the output
$output = '<?xml version="1.0"?>';
$output .= '<Events>';
$output .= '<Status>'.$h->status.'</Status>';
$output .= '<Course>'.$course.'</Course>';
$output .= '<Year>'.$year.'</Year>';

foreach($results as $result){
    // Filter out past events
    if($result->getEnd() >= strtotime(date("Y-m-d 00:00:00", time()))){
        $output .= $result->toXml();
    }
}

$output .= "</Events>";

header ("Content-Type:text/xml");
echo $output;
exit();

?>